package com.esyoga.yoyoga.service.impl;

import com.esyoga.yoyoga.mapper.*;
import com.esyoga.yoyoga.pojo.*;
import com.esyoga.yoyoga.service.TrainerService;
import lombok.Data;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Data
@Service("trainerService")
@Transactional
public class TrainerServiceImpl implements TrainerService {
    @Resource
    TContractMapper tContractMapper;
    @Resource
    TStudentMapper tStudentMapper;
    @Resource
    TContractExample tContractExample;
    @Resource
    TStudentExample tStudentExample;
    @Resource
    TTrainerMapper tTrainerMapper;
    @Resource
    TTrainerExample tTrainerExample;
    @Resource
    TTrainerAgreementMapper trainerAgreementMapper;
    @Resource
    TTrainerAgreementExample trainerAgreementExample;
    @Resource
    TGymMapper tGymMapper;
    @Resource
    TGymExample tGymExample;

    /**
     * 通过合约状态和教练id查出所有该状态下的学生
     * @param cState   合约状态信息
     * @param tid       教练id
     * @return
     */
    @Override
    public List<TStudent> getStudentsByCStateAndTid(int cState, int tid) {
        tContractExample.clear();
        tContractExample.createCriteria().andCStateEqualTo(cState).andCTIdEqualTo(tid);
        List<TContract> contracts = tContractMapper.selectByExample(tContractExample);
        List<TStudent> students = new ArrayList<>();
        for (TContract contract:contracts) {
            int sId = contract.getcSId();
            TStudent student = tStudentMapper.selectByPrimaryKey(sId);
            students.add(student);
        }
        return students;
    }


    @Override
    public List<TTrainer> selectTrainers(String tname) {
        tTrainerExample.clear();
        TTrainerExample.Criteria criteria  =tTrainerExample.createCriteria();
        criteria.andTNameLike(tname);
        List<TTrainer> trainers = tTrainerMapper.selectByExample(tTrainerExample);
        return trainers;
    }

    @Override
    public String updateTrainerBalance(BigDecimal money, Integer tId) {
        //获取教练当前余额
        BigDecimal trainerBalance = tTrainerMapper.selectByPrimaryKey(tId).gettBanlance();
        //更新的金额
        BigDecimal trainerUpdateBalance = trainerBalance.add(money);
        //余额不足
        if(trainerUpdateBalance.compareTo(new BigDecimal(0))<0){
            return "更新失败，余额不足";
        }
        //更新
        TTrainer tTrainer = new TTrainer();
        tTrainer.settId(tId);
        tTrainer.settBanlance(trainerUpdateBalance);
        if(tTrainerMapper.updateByPrimaryKeySelective(tTrainer)>0){
            return "更新成功";
        }else{
            return "更新失败";
        }
    }

    /**
     * 根据教练ID查找其签约成功的场馆
     * @param tId
     * @return
     */
    @Override
    public List<TGym> selectGymBytId(Integer tId) {
        List<Integer> gIds=new ArrayList<Integer>();
        //通过教练id在私教和场馆签约表查询该场馆下的所有场馆，获得其id，在查询他们的信息
        trainerAgreementExample.clear();
        TTrainerAgreementExample.Criteria criteria= trainerAgreementExample.createCriteria();
        criteria.andTaTIdEqualTo(tId);
        //此合约状态必须为签约成功
        criteria.andTaStateEqualTo(4);
        //获取所有合约信息，取出场馆ID
        List<TTrainerAgreement> tTrainerAgreements= trainerAgreementMapper.selectByExample(trainerAgreementExample);
        for (TTrainerAgreement tTrainerAgreement:tTrainerAgreements
        ) {
            gIds.add(tTrainerAgreement.getTaGId());
        }
        //将上面已获得该场馆下私教的所有id用来查询,并且此私教状态审核通过的
        tGymExample.clear();
        TGymExample.Criteria criteria1= tGymExample.createCriteria();
        criteria1.andGIdIn(gIds);
        criteria1.andGStateEqualTo(0);
        List<TGym> tGyms= tGymMapper.selectByExample(tGymExample);
        return tGyms;
    }

    @Override
    public TTrainer selectTrainerById(Integer tId) {
        return tTrainerMapper.selectByPrimaryKey(tId);
    }

}
